// StereoMaker
// Copyright(C) 2012 Vadim Trochinsky
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
#include "stereo.h"


void help(char *progname) {
	printf("Syntax: %s <options>\n\n", progname);
	printf("\t--left <image>     Left image\n");
	printf("\t--right <image>    Right image\n");
	printf("\t--both <image>     Both images in one\n");
	printf("\t--middle-crop <n>  Amount to crop from the middle for --both\n");
	printf("\t--left <image>     Left image\n");
	printf("\t--type <type>      Output type\n");
	printf("\t--flip             Flip left/right\n");
	printf("\t--format <fmt>     Output format: png, jpg\n");
	printf("\t--out <image>      Output image\n");
	printf("\n");


}

int main(int argc, char **argv) {
	StereoGenerator *stereo = NULL;
	int i = 0;

	char *output = NULL;
	int default_format = STEREO_FMT_JPEG;
	int format = 0;
	int flip = 0;
	int type = STEREO_ANAGLYPH;

	if ( argc < 4 ) {
		help(argv[0]);
		exit(1);
	}

	stereo = StereoAlloc();

	for(i=1;i<argc;i++) {
		if ( i == argc - 1 ) {
			fprintf(stderr, "Argument required for parameter %s\n", argv[i]);
//			exit(1);
		}

		fprintf(stderr, "ARG: %s\n", argv[i]);

		if (!strcmp(argv[i], "--left")) {
			StereoSetFile(stereo, STEREO_LEFT, argv[++i]);
		} else if (!strcmp(argv[i], "--right")) {
			StereoSetFile(stereo, STEREO_RIGHT, argv[++i]);
		} else if (!strcmp(argv[i], "--both")) {
			StereoSetFile(stereo, STEREO_COMBINED, argv[++i]);
		} else if (!strcmp(argv[i], "--middle-crop")) {
			StereoSetMiddleCutoff(stereo, atoi(argv[++i]));
		} else if (!strcmp(argv[i], "--out")) {
			output = argv[++i];
		} else if (!strcmp(argv[i], "--flip")) {
			flip = 1;
		} else if (!strcmp(argv[i], "--format")) {
			i++;
			if (!strcmp(argv[i], "png")) {
				format = STEREO_FMT_PNG;
			} else if (!strcmp(argv[i], "jpg")) {
				format = STEREO_FMT_JPEG;
			} else {
				fprintf(stderr, "Unknown format %s\n", argv[i]);
				exit(1);
			}
		} else if (!strcmp(argv[i], "--type")) {
			i++;
			if (!strcmp(argv[i], "anaglyph")) {
				type = STEREO_ANAGLYPH;
			} else if (!strcmp(argv[i], "horizontal")) {
				type = STEREO_HORIZONTAL_INTERLACED;
				default_format = STEREO_FMT_PNG;
			} else if (!strcmp(argv[i], "vertical")) {
				type = STEREO_VERTICAL_INTERLACED;
				default_format = STEREO_FMT_PNG;
			} else if (!strcmp(argv[i], "cross-eyed")) {
				type = STEREO_CROSS_EYED;
			} else if (!strcmp(argv[i], "checkerboard")) {
				type = STEREO_CHECKERBOARD;

			} else {
				fprintf(stderr, "Unknown image type %s\n", argv[i]);
				exit(1);
			}
		} else if (!strcmp(argv[i], "--help")) {
			help(argv[0]);
			exit(0);
		}
	}

	if ( format == 0 ) {
		format = default_format;
	}
	
	if (flip) {
		type |= STEREO_FLIP;
	}

	if ( StereoCompute(stereo, type) == STEREO_OK ) {
		StereoWriteOutput(stereo, output, format);
	} else {
		fprintf(stderr, "Failed to generate stereo\n");
	}
	

}
